##### R CODE TO REPLICATE THE TABLES INCLUDED IN 
##### Adrian Lucardi and Juan Pablo Micozzi, "The Effect of the Electoral Cycle on Legislators’ Career Strategies. Evidence from Argentina, 1983-2007," Legislative Studies Quarterly, 41(4), 2016, 811-840.

## Loading the required R packages. Make sure to have them installed before proceeding. In order to install a package called X, write install.packages("X")
library (car)
library (faraway)
library (foreign)
library (ggplot2)
library (gridExtra)
library (lmtest)
library (MASS)
library (multiwayvcov)
library (xtable)

## display options
options (digits=5, scipen=6, show.signif.stars=FALSE)

## working directory -> change this to the directory where your have your data
setwd ("/Users/adrianlucardi/Documents/WashU/Research/2013 Electoral Cycle/Data/")



###### DOWNLOADING AND CLEANING THE DATA

## downloading the data
baseFull <- read.csv ("Argentina Deputies 1983-2007 by mandate.csv", sep=",", header=TRUE)


## creating some additional variables

# bill submission
baseFull$Nbills1.prop <- with (baseFull, ifelse (Nbills==0, 0, Nbills1 / Nbills))
baseFull$provorig1.prop <- with (baseFull, ifelse (provorig==0, 0, provorig1 / provorig))
baseFull$muniorigen1.prop <- with (baseFull, ifelse (muniorigen==0, 0, muniorigen1 / muniorigen))
baseFull$provorigD <- with (baseFull, ifelse (provorig > 0, 1, 0))
baseFull$muniorigenD <- with (baseFull, ifelse (muniorigen > 0, 1, 0))

# executive candidacies
baseFull$execcandt1 <- with (baseFull, ifelse (govcandt1b==1 | mayorcandt1==1, 1, 0))
baseFull$wonexect1 <- with (baseFull, ifelse (wongovt1b==1 | wonmayor==1, 1, 0))
baseFull$lostgovt1 <- with (baseFull, ifelse (is.na (govcandt1), NA
                                            , ifelse (govcandt1==1 & wongov==0, 1, 0)))
baseFull$lostgovt1b <- with (baseFull, ifelse (is.na (govcandt1b), NA
                                            , ifelse (govcandt1b==1 & wongovt1b==0, 1, 0)))
baseFull$lostgovt2b <- with (baseFull, ifelse (is.na (govcandt2b), NA
                                            , ifelse (govcandt2b==1 & wongovt2b==0, 1, 0)))
baseFull$lostmayor <- with (baseFull, ifelse (is.na (mayorcandt1), NA
                                            , ifelse (mayorcandt1==1 & wonmayor==0, 1, 0)))
baseFull$lostexect1 <- with (baseFull, ifelse (is.na (execcandt1), NA
                                              , ifelse (execcandt1==1 & wonexect1==0, 1, 0)))

# executive experience
baseFull$Exec.experience <- with (baseFull, ifelse (exec.before==1 | prevmayor==1, 1, 0))
baseFull$Exec.experience1 <- with (baseFull, ifelse (gov.before==1, 1
                                                     , ifelse (vice.before==1, 0.5
                                                               , ifelse (prevmayor==1, prevmayor*ratiomunipop, 0)))) ##i.e., former governors are half as important as former vice-governors, and mayors are weighted according to the relative size of their municipality
baseFull$Exec.experience2 <- with (baseFull, ifelse (gov.before==1, 1
                                                     , ifelse (vice.before==1, 0.5^2
                                                               , ifelse (prevmayor==1, prevmayor*(ratiomunipop^2), 0))))
baseFull$Exec.experience.ord <- with (baseFull, factor (ifelse (prevmayor==1, "03 Mayor"
                                                        , ifelse (vice.before==1, "02 Vicegovernor"
                                                        , ifelse (gov.before==1, "01 Governor", "04 None")))))
baseFull$Exec.experience.ord <- factor (baseFull$Exec.experience.ord, levels=sort (unique (as.character (baseFull$Exec.experience.ord)), decreasing=TRUE), ordered=TRUE)  ## an ordered version of the previous executive experience variable

baseFull$uniqueID <- factor (baseFull$uniqueID)
baseFull$name <- factor (baseFull$name)
baseFull$district <- factor (baseFull$district)
baseFull$period <- factor (baseFull$period)
summary (baseFull); dim (baseFull)


## Removing NAs and other unused variables:
base <- baseFull[baseFull$Nterms.before==0 & baseFull$replacement==0 & baseFull$begin >= 1985,] ## deputies in the first term in office, who were not replacements. We begin in 1985 so that former governors had an opportunity to run for legislative office

# re-factorizing
base$replacement <- NULL
base$uniqueID <- factor (base$uniqueID)
base$name <- factor (base$name)
base$district <- factor (base$district)
base$period <- factor (base$period)
summary (base); dim (base)

## 1983 cohort
base83 <- baseFull[baseFull$replacement==0 & baseFull$begin==1983,]

# re-factorizing
base83$replacement <- NULL
base83$uniqueID <- factor (base83$uniqueID)
base83$name <- factor (base83$name)
base83$district <- factor (base83$district)
base83$period <- factor (base83$period)
summary (base83); dim (base83)



########## FITTING THE MODELS


### Replicating the models in Table 1

# model 1.1
summary (mod101a <- glm (Exec.experience ~ midterm, family=binomial (link="logit"), data=base))
coeftest (mod101a, vcov=cluster.vcov (mod101a, base$district))

# model 1.2
summary (mod101b <- glm (Exec.experience ~ midterm + female + log (mag), family=binomial (link="logit"), data=base))
coeftest (mod101b, vcov=cluster.vcov (mod101b, base$district))

# model 1.3
summary (mod101j <- glm (Exec.experience ~ midterm + female + log (mag) + bloque_pj + bloque_ucr, family=binomial (link="logit"), data=base))
coeftest (mod101j, vcov=cluster.vcov (mod101j, base$district))

# model 1.4
summary (mod103a2 <- glm (gov.before ~ midterm + log (mag) + bloque_pj + bloque_ucr, family=binomial (link="logit"), data=base))  ## we have to exclude female because of collinearity issues
coeftest (mod103a2, vcov=cluster.vcov (mod103a2, base$district))

# model 1.5
summary (mod103b2 <- glm (exec.before ~ midterm + female + log (mag) + bloque_pj + bloque_ucr, family=binomial (link="logit"), data=base))
coeftest (mod103b2, vcov=cluster.vcov (mod103b2, base$district))

# model 1.6
summary (mod103c2 <- glm (prevmayor ~ midterm + female + log (mag) + bloque_pj + bloque_ucr, family=binomial (link="logit"), data=base))
coeftest (mod103c2, vcov=cluster.vcov (mod103c2, base$district))

# model 1.7
summary (mod103e2 <- lm (Exec.experience ~ midterm + female + log (mag) + bloque_pj + bloque_ucr, data=base))
coeftest (mod103e2, vcov=cluster.vcov (mod103e2, base$district))

# model 1.8
summary (mod103f2 <- lm (Exec.experience1 ~ midterm + female + log (mag) + bloque_pj + bloque_ucr, data=base))
coeftest (mod103f2, vcov=cluster.vcov (mod103f2, base$district))

# model 1.9
summary (mod103g2 <- lm (Exec.experience2 ~ midterm + female + log (mag) + bloque_pj + bloque_ucr, data=base))
coeftest (mod103g2, vcov=cluster.vcov (mod103g2, base$district))



### Replicating the models in Table 2

# Panel (a): Bill submission

# model 2.1
summary (mod203a4 <- glm.nb (Nbills1 ~ midterm + female + log (mag) + bloque_pj + bloque_ucr + Exec.experience + compresi, data=base))
coeftest (mod203a4, vcov=cluster.vcov (mod203a4, base$district))

# model 2.2
summary (mod203b4 <- glm.nb (provorig1 ~ midterm + female + log (mag) + bloque_pj + bloque_ucr + Exec.experience + compresi, data=base))
coeftest (mod203b4, vcov=cluster.vcov (mod203b4, base$district))

# model 2.3
summary (mod203c4 <- glm.nb (muniorigen1 ~ midterm + female + log (mag) + bloque_pj + bloque_ucr + Exec.experience + compresi, data=base))
coeftest (mod203c4, vcov=cluster.vcov (mod203c4, base$district))

# model 2.4
summary (mod201j2 <- glm.nb (Nbills ~ midterm + female + log (mag) + bloque_pj + bloque_ucr + Exec.experience + compresi, data=base[!is.na (base$Nbills1),]))
coeftest (mod201j2, vcov=cluster.vcov (mod201j2, base[!is.na (base$Nbills1),]$district))

# model 2.5
summary (mod203b2 <- glm.nb (provorig ~ midterm + female + log (mag) + bloque_pj + bloque_ucr + Exec.experience + compresi, data=base[!is.na (base$Nbills1),]))
coeftest (mod203b2, vcov=cluster.vcov (mod203b2, base[!is.na (base$Nbills1),]$district))

# model 2.6
summary (mod203c2 <- glm.nb (muniorigen ~ midterm + female + log (mag) + bloque_pj + bloque_ucr + Exec.experience + compresi, data=base[!is.na (base$Nbills1),]))
coeftest (mod203c2, vcov=cluster.vcov (mod203c2, base[!is.na (base$Nbills1),]$district))

# model 2.7
summary (mod203a10 <- glm.nb (Nbills1 ~ midterm + female + log (mag) + bloque_pj + bloque_ucr + compresi, data=base83))
coeftest (mod203a10, vcov=cluster.vcov (mod203a10, base83$district))

# model 2.8
summary (mod203b4d2 <- glm.nb (provorig1 ~ midterm + female + log (mag) + bloque_pj + bloque_ucr + compresi, data=base83))
coeftest (mod203b4d2, vcov=cluster.vcov (mod203b4d2, base83$district))

# model 2.9
summary (mod203c4d2 <- glm.nb (muniorigen1 ~ midterm + female + log (mag) + bloque_pj + bloque_ucr + compresi, data=base83)) ## no differences here
coeftest (mod203c4d2, vcov=cluster.vcov (mod203c4d2, base83$district))


# Panel (b): Reelection

# model 3.1
summary (mod301 <- glm (runReel ~ midterm, family=binomial (link="logit"), data=base[base$died==0 & base$resigned==0 & base$wonexect1==0,]))
coeftest (mod301, vcov=cluster.vcov (mod301, base[base$died==0 & base$resigned==0 & base$wonexect1==0,]$district))

# model 3.2
summary (mod301a <- glm (runReel ~ midterm * lostexect1, family=binomial (link="logit"), data=base[base$died==0 & base$resigned==0 & base$wonexect1==0,]))
coeftest (mod301a, vcov=cluster.vcov (mod301a, base[base$died==0 & base$resigned==0 & base$wonexect1==0,]$district))

# model 3.3
summary (mod301j2 <- glm (runReel ~ midterm * lostexect1 + female + log (mag) + bloque_pj + bloque_ucr, family=binomial (link="logit"), data=base[base$died==0 & base$resigned==0 & base$wonexect1==0,]))
coeftest (mod301j2, vcov=cluster.vcov (mod301j2, base[base$died==0 & base$resigned==0 & base$wonexect1==0,]$district))

# model 3.4
summary (mod303a2 <- glm (runReel ~ midterm * lostgovt1b + female + log (mag) + bloque_pj + bloque_ucr, family=binomial (link="logit"), data=base[base$died==0 & base$resigned==0 & base$wonexect1==0,]))
coeftest (mod303a2, vcov=cluster.vcov (mod303a2, base[base$died==0 & base$resigned==0 & base$wonexect1==0,]$district))

# model 3.5
summary (mod303c2 <- glm (runReel ~ midterm * lostmayor + female + log (mag) + bloque_pj + bloque_ucr, family=binomial (link="logit"), data=base[base$died==0 & base$resigned==0 & base$wonexect1==0,]))
coeftest (mod303c2, vcov=cluster.vcov (mod303c2, base[base$died==0 & base$resigned==0 & base$wonexect1==0,]$district))

# model 3.6
summary (mod304a <- glm (reelected ~ midterm * lostexect1, family=binomial (link="logit"), data=base[base$died==0 & base$resigned==0 & base$wonexect1==0,]))
coeftest (mod304a, vcov=cluster.vcov (mod304a, base[base$died==0 & base$resigned==0 & base$wonexect1==0,]$district))

# model 3.7
summary (mod304j2 <- glm (reelected ~ midterm * lostexect1 + female + log (mag) + bloque_pj + bloque_ucr, family=binomial (link="logit"), data=base[base$died==0 & base$resigned==0 & base$wonexect1==0,]))
coeftest (mod304j2, vcov=cluster.vcov (mod304j2, base[base$died==0 & base$resigned==0 & base$wonexect1==0,]$district))

# model 3.8
summary (mod302j <- glm (runReel ~ midterm, family=binomial (link="logit"), data=base83[base83$died==0 & base83$resigned==0,]))
coeftest (mod302j, vcov=cluster.vcov (mod302j, base83[base83$died==0 & base83$resigned==0,]$district))

# model 3.9
summary (mod302j5 <- glm (runReel ~ midterm + female + log (mag) + bloque_pj + bloque_ucr, family=binomial (link="logit"), data=base83[base83$died==0 & base83$resigned==0,]))
coeftest (mod302j5, vcov=cluster.vcov (mod302j5, base83[base83$died==0 & base83$resigned==0,]$district))




########## BUILDING THE FIGURES

### Replicating Figure 1

set.seed (56156) ## for reproducibility
sims <- 1000

# (1) Self-selection
(count01 <- cbind (c (1, 0, 0, log (3), 1, 0)
                   , c (1, 1, 0, log (3), 1, 0)))
sims.coef01 <- mvrnorm (n=sims, mu=mod101j$coef, Sigma=cluster.vcov (mod101j, base$district))

sims.pred01conc <- ilogit (sims.coef01 %*% count01[,1])  ##concurrent
sims.pred01mid <- ilogit (sims.coef01 %*% count01[,2])  ##midterm

# (2) Bill submission
(count02a <- cbind (c (1, 0, 0, log (3), 1, 0, 0, 0)
                    , c (1, 1, 0, log (3), 1, 0, 0, 0)))
sims.coef02a <- mvrnorm (n=sims, mu=mod203a4$coef, Sigma=cluster.vcov (mod203a4, base$district))

sims.pred02aconc <- exp (sims.coef02a %*% count02a[,1])  ##concurrent
sims.pred02amid <- exp (sims.coef02a %*% count02a[,2])  ##midterm

# (3) Reelection
(count03 <- cbind (c (1, 0, 0, 0, log (3), 1, 0, 0)
                   , c (1, 0, 1, 0, log (3), 1, 0, 0)
                   , c (1, 1, 0, 0, log (3), 1, 0, 0)
                   , c (1, 1, 1, 0, log (3), 1, 0, 1) ))
sims.coef03 <- mvrnorm (n=sims, mu=mod301j2$coef, Sigma=cluster.vcov (mod301j2, base[base$died==0  & base$resigned==0 & base$wonexect1==0,]$district))

sims.pred03conc0 <- ilogit (sims.coef03 %*% count03[,1])  ##concurrent, not won
sims.pred03conc1 <- ilogit (sims.coef03 %*% count03[,2])  ##concurrent, won
sims.pred03mid0 <- ilogit (sims.coef03 %*% count03[,3])  ##midterm, not won
sims.pred03mid1 <- ilogit (sims.coef03 %*% count03[,4])  ##midterm, won

# Predicted values Hypothesis 1
(h01 <- as.data.frame (rbind (
  c (quantile (sims.pred01conc, probs=c(0.05, 0.10)), mean (sims.pred01conc), quantile (sims.pred01conc, probs=c(0.90, 0.95)))
  , c (quantile (sims.pred01mid, probs=c(0.05, 0.10)), mean (sims.pred01mid), quantile (sims.pred01mid, probs=c(0.90, 0.95)))
)))
colnames (h01) <- c ("low95", "low90", "med", "high90", "high95")
h01$midterm <- c (0, 1)
h01$midterm2 <- factor (c ("concurrent", "midterm"))

# Predicted values Hypothesis 2
(h02 <- as.data.frame (rbind (
  c (quantile (sims.pred02aconc, probs=c(0.05, 0.10)), mean (sims.pred02aconc), quantile (sims.pred02aconc, probs=c(0.90, 0.95)))
  , c (quantile (sims.pred02amid, probs=c(0.05, 0.10)), mean (sims.pred02amid), quantile (sims.pred02amid, probs=c(0.90, 0.95)))
)))
colnames (h02) <- c ("low95", "low90", "med", "high90", "high95")
h02$midterm <- c (0, 1)
h02$midterm2 <- factor (c ("concurrent", "midterm"))

# Predicted values Hypothesis 3
(h03 <- as.data.frame (rbind (
  c (quantile (sims.pred03conc0, probs=c(0.05, 0.10)), mean (sims.pred03conc0), quantile (sims.pred03conc0, probs=c(0.90, 0.95)))
  , c (quantile (sims.pred03conc1, probs=c(0.05, 0.10)), mean (sims.pred03conc1), quantile (sims.pred03conc1, probs=c(0.90, 0.95)))
  , c (quantile (sims.pred03mid0, probs=c(0.05, 0.10)), mean (sims.pred03mid0), quantile (sims.pred03mid0, probs=c(0.90, 0.95)))
  , c (quantile (sims.pred03mid1, probs=c(0.05, 0.10)), mean (sims.pred03mid1), quantile (sims.pred03mid1, probs=c(0.90, 0.95)))
)))
colnames (h03) <- c ("low95", "low90", "med", "high90", "high95")
h03$midterm <- c (0, 0, 1, 1)
h03$cases <- factor (c ("concurrent", "concurrent,\nlost election", "midterm", "midterm,\nlost election"))


## Making the plots
cex.points <- 4
cex.text <- 20
col.fill <- "black"
cex.bars <- 0.075

# Panel (a)
(p01ci95 <- ggplot (h01, aes (x=midterm2, y=med)) +
   geom_errorbar (width=cex.bars, aes (ymin=low95, ymax=high95)) +
   geom_point (shape=21, size=cex.points, fill=col.fill) +
   ggtitle (expression (paste ("(a) ", H[1], ". Self-selection"))) +
   theme_bw () +
   theme (text=element_text (size=cex.text)) +
   xlab ("") +
   ylab ("Pr (Former executive = 1)") +
   ylim (0.15, 0.45))

# Panel (b)
(p02ci95 <- ggplot (h02, aes (x=midterm2, y=med)) +
   geom_errorbar (width=cex.bars, aes (ymin=low95, ymax=high95)) +
   geom_point (shape=21, size=cex.points, fill=col.fill) +
   ggtitle (expression (paste ("(b) ", H[2], ". Bill submission"))) +
   theme_bw () +
   theme (text=element_text (size=cex.text)) +
   xlab ("") +
   ylab ("Bills submited (first term)") +
   ylim (20, 35))

# Panel (c)
(p03ci95 <- ggplot (h03, aes (x=cases, y=med)) +
   geom_errorbar (width=cex.bars, aes (ymin=low95, ymax=high95)) +
   geom_point (shape=21, size=cex.points, fill=col.fill) +
   ggtitle (expression (paste ("(c) ", H[3], ". Reelection"))) +
   theme_bw () +
   theme (text=element_text (size=cex.text)) +
   xlab ("") +
   ylab ("Pr (Reelection = 1)") +
   ylim (0.05, 0.55))


## Exporting the plots

# All together
(aspect.ratio <- 3/1)
plotWidth <- 1350
png ("fig_results95.png", w=plotWidth, h=plotWidth/aspect.ratio)
grid.arrange (arrangeGrob (p01ci95, p02ci95, p03ci95, ncol=3, widths=c(1/3,1/3,1/3)))
dev.off ()

# Separately
(aspect.ratio <- 1.25/1)
plotWidth <- 640

png ("fig_results0195.png", w=plotWidth, h=plotWidth/aspect.ratio)
p01ci95
dev.off ()

png ("fig_results0295.png", w=plotWidth, h=plotWidth/aspect.ratio)
p02ci95
dev.off ()

png ("fig_results0395.png", w=plotWidth, h=plotWidth/aspect.ratio)
p03ci95
dev.off ()




########## BUILDING THE TABLES

### Replicating Table 1
(tab.coefs01 <- cbind (
  c (coeftest (mod101a, vcov=cluster.vcov (mod101a, base$district))[,1], rep (NA, 4))
  , c (coeftest (mod101b, vcov=cluster.vcov (mod101b, base$district))[,1], rep (NA, 2))
  , c (coeftest (mod101j, vcov=cluster.vcov (mod101j, base$district))[,1])
  , c (coeftest (mod103a2, vcov=cluster.vcov (mod103a2, base$district))[1:2,1], NA, coeftest (mod103a2, vcov=cluster.vcov (mod103a2, base$district))[3:5,1])
  , c (coeftest (mod103b2, vcov=cluster.vcov (mod103b2, base$district))[,1])
  , c (coeftest (mod103c2, vcov=cluster.vcov (mod103c2, base$district))[,1])
  , c (coeftest (mod103e2, vcov=cluster.vcov (mod103e2, base$district))[,1])
  , c (coeftest (mod103f2, vcov=cluster.vcov (mod103f2, base$district))[,1])
  , c (coeftest (mod103g2, vcov=cluster.vcov (mod103g2, base$district))[,1]) ))
(tab.ses01 <- cbind (
  c (coeftest (mod101a, vcov=cluster.vcov (mod101a, base$district))[,2], rep (NA, 4))
  , c (coeftest (mod101b, vcov=cluster.vcov (mod101b, base$district))[,2], rep (NA, 2))
  , c (coeftest (mod101j, vcov=cluster.vcov (mod101j, base$district))[,2])
  , c (coeftest (mod103a2, vcov=cluster.vcov (mod103a2, base$district))[1:2,2], NA, coeftest (mod103a2, vcov=cluster.vcov (mod103a2, base$district))[3:5,2])
  , c (coeftest (mod103b2, vcov=cluster.vcov (mod103b2, base$district))[,2])
  , c (coeftest (mod103c2, vcov=cluster.vcov (mod103c2, base$district))[,2])
  , c (coeftest (mod103e2, vcov=cluster.vcov (mod103e2, base$district))[,2])
  , c (coeftest (mod103f2, vcov=cluster.vcov (mod103f2, base$district))[,2])
  , c (coeftest (mod103g2, vcov=cluster.vcov (mod103g2, base$district))[,2]) ))

odd <- seq (1, nrow (tab.coefs01)*2, by=2)
even <- seq (2, nrow (tab.coefs01)*2, by=2)

(tab.results01 <- data.frame (rbind (tab.coefs01, tab.ses01)))
rownames (tab.results01) <- c (odd, even)
(tab.results01 <- tab.results01[order (as.numeric (rownames (tab.results01))),])

rows <- c ("Intercept", "", "Midterm", "", "Female", "", "Magnitude (log)", "", "PJ bloc", "", "UCR bloc", "")

(aics <- c (AIC (mod101a), AIC (mod101b), AIC (mod101j), AIC (mod103a2), AIC (mod103b2), AIC (mod103c2), AIC (mod103e2), AIC (mod103f2), AIC (mod103g2)))
(bics <- c (BIC (mod101a), BIC (mod101b), BIC (mod101j), BIC (mod103a2), BIC (mod103b2), BIC (mod103c2), BIC (mod103e2), BIC (mod103f2), BIC (mod103g2)))
(logliks <- c (logLik (mod101a), logLik (mod101b), logLik (mod101j), logLik (mod103a2), logLik (mod103b2), logLik (mod103c2), logLik (mod103e2), logLik (mod103f2), logLik (mod103g2)))
(deviances <- c (deviance (mod101a), deviance (mod101b), deviance (mod101j), deviance (mod103a2), deviance (mod103b2), deviance (mod103c2), deviance (mod103e2), deviance (mod103f2), deviance (mod103g2)))

(n.obs <- c (mod101a$df.null + 1, mod101b$df.null + 1, mod101j$df.null + 1, mod103a2$df.null + 1, mod103b2$df.null + 1, mod103c2$df.null + 1, length (mod103e2$fitted.values), length (mod103f2$fitted.values), length (mod103g2$fitted.values)))

Header0 <- paste ("\\multicolumn{10}{c}{} \\\\ \n")
Header0l <- paste ("\\hline \\multicolumn{10}{c}{} \\\\ \n")
Header1 <- paste (" & \\multicolumn{3}{c}{Former} & \\multicolumn{1}{c}{gov.} & \\multicolumn{1}{c}{gov. or} & \\multicolumn{1}{c}{mayor} & \\multicolumn{3}{c}{Former executive} \\\\ \n")
Header2 <- paste (" & \\multicolumn{3}{c}{executive} & \\multicolumn{1}{c}{only*} & \\multicolumn{1}{c}{vice} & \\multicolumn{1}{c}{only} & \\multicolumn{1}{c}{(OLS)} & \\multicolumn{1}{c}{(W)} & \\multicolumn{1}{c}{(W$^2$)} \\\\ \n")
Header3 <- paste (" & \\multicolumn{1}{c}{(1.1)} & \\multicolumn{1}{c}{(1.2)} & \\multicolumn{1}{c}{(1.3)} & \\multicolumn{1}{c}{(1.4)} & \\multicolumn{1}{c}{(1.5)} & \\multicolumn{1}{c}{(1.6)} & \\multicolumn{1}{c}{(1.7)} & \\multicolumn{1}{c}{(1.8)} & \\multicolumn{1}{c}{(1.9)} \\\\ \\hline \\hline \n")
add1 <- paste ("AIC & \\multicolumn{1}{c}{", round (aics[1], 1), "} & \\multicolumn{1}{c}{", round (aics[2], 1), "} & \\multicolumn{1}{c}{", round (aics[3], 1), "} & \\multicolumn{1}{c}{", round (aics[4], 1), "} & \\multicolumn{1}{c}{", round (aics[5], 1), "} & \\multicolumn{1}{c}{", round (aics[6], 1), "} & \\multicolumn{1}{c}{", round (aics[7], 1), "} & \\multicolumn{1}{c}{", round (aics[8], 1), "} & \\multicolumn{1}{c}{", round (aics[9], 1), "} \\\\ \n")
add2 <- paste ("BIC & \\multicolumn{1}{c}{", round (bics[1], 1), "} & \\multicolumn{1}{c}{", round (bics[2], 1), "} & \\multicolumn{1}{c}{", round (bics[3], 1), "} & \\multicolumn{1}{c}{", round (bics[4], 1), "} & \\multicolumn{1}{c}{", round (bics[5], 1), "} & \\multicolumn{1}{c}{", round (bics[6], 1), "} & \\multicolumn{1}{c}{", round (bics[7], 1), "} & \\multicolumn{1}{c}{", round (bics[8], 1), "} & \\multicolumn{1}{c}{", round (bics[9], 1), "} \\\\ \n")
add3 <- paste ("log-Likelihood & \\multicolumn{1}{c}{", round (logliks[1], 1), "} & \\multicolumn{1}{c}{", round (logliks[2], 1), "} & \\multicolumn{1}{c}{", round (logliks[3], 1), "} & \\multicolumn{1}{c}{", round (logliks[4], 1), "} & \\multicolumn{1}{c}{", round (logliks[5], 1), "} & \\multicolumn{1}{c}{", round (logliks[6], 1), "} & \\multicolumn{1}{c}{", round (logliks[7], 1), "} & \\multicolumn{1}{c}{", round (logliks[8], 1), "} & \\multicolumn{1}{c}{", round (logliks[9], 1), "} \\\\ \n")
add4 <- paste ("Deviance & \\multicolumn{1}{c}{", round (deviances[1], 1), "} & \\multicolumn{1}{c}{", round (deviances[2], 1), "} & \\multicolumn{1}{c}{", round (deviances[3], 1), "} & \\multicolumn{1}{c}{", round (deviances[4], 1), "} & \\multicolumn{1}{c}{", round (deviances[5], 1), "} & \\multicolumn{1}{c}{", round (deviances[6], 1), "} & \\multicolumn{1}{c}{", round (deviances[7], 1), "} & \\multicolumn{1}{c}{", round (deviances[8], 1), "} & \\multicolumn{1}{c}{", round (deviances[9], 1), "} \\\\ \n")
add5 <- paste ("$N$ & \\multicolumn{1}{c}{", round (n.obs[1], 1), "} & \\multicolumn{1}{c}{", round (n.obs[2], 1), "} & \\multicolumn{1}{c}{", round (n.obs[3], 1), "} & \\multicolumn{1}{c}{", round (n.obs[4], 1), "} & \\multicolumn{1}{c}{", round (n.obs[5], 1), "} & \\multicolumn{1}{c}{", round (n.obs[6], 1), "} & \\multicolumn{1}{c}{", round (n.obs[7], 1), "} & \\multicolumn{1}{c}{", round (n.obs[8], 1), "} & \\multicolumn{1}{c}{", round (n.obs[9], 1), "} \\\\ \n")
add6 <- paste ("Specification & \\multicolumn{1}{c}{logit} & \\multicolumn{1}{c}{logit} & \\multicolumn{1}{c}{logit} & \\multicolumn{1}{c}{logit} & \\multicolumn{1}{c}{logit} & \\multicolumn{1}{c}{logit} & \\multicolumn{1}{c}{OLS} & \\multicolumn{1}{c}{OLS} & \\multicolumn{1}{c}{OLS} \\\\ \\hline \n")
Bottom1 <- paste ("\\multicolumn{10}{l}{($^*$) A dummy for \\emph{Female} is not included because of perfect collinearity with the outcome.} \\\\ \n")
Bottom2 <- paste ("\\multicolumn{10}{l}{(Clustered standard errors in parentheses.)} \\\\ \n")

addtorow <- list()
addtorow$pos <- list()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 12
addtorow$pos[[6]] <- 12
addtorow$pos[[7]] <- 12
addtorow$pos[[8]] <- 12
addtorow$pos[[9]] <- 12
addtorow$pos[[10]] <- 12
addtorow$pos[[11]] <- 12
addtorow$pos[[12]] <- 12
addtorow$pos[[13]] <- 12
addtorow$pos[[14]] <- 12
addtorow$command <- c (Header0, Header1, Header2, Header3
                       , Header0l, add1, add2, add3, add4
                       , Header0, add5, add6, Bottom1, Bottom2)
print (xtable (  cbind (rows, tab.results01)
                 , align=c("l","l","c","c","c","c","c","c","c","c","c")
                 , digits=2
                 , caption="{\\small The electoral calendar and the self-selection of politicians into legislative cohorts in Argentina, 1985-2007.}"
                 , label="T:results01")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow  ) ## copy and paste in LaTeX



### Replicating Table 2

# Panel (a)
(tab.coefs02 <- cbind (
  c (coeftest (mod203a4, vcov=cluster.vcov (mod203a4, base$district))[,1])
  , c (coeftest (mod203b4, vcov=cluster.vcov (mod203b4, base$district))[,1])
  , c (coeftest (mod203c4, vcov=cluster.vcov (mod203c4, base$district))[,1])
  , c (coeftest (mod201j2, vcov=cluster.vcov (mod201j2, base[!is.na (base$Nbills1),]$district))[,1])
  , c (coeftest (mod203b2, vcov=cluster.vcov (mod203b2, base[!is.na (base$Nbills1),]$district))[,1])
  , c (coeftest (mod203c2, vcov=cluster.vcov (mod203c2, base[!is.na (base$Nbills1),]$district))[,1])
  , c (coeftest (mod203a10, vcov=cluster.vcov (mod203a10, base83$district))[1:6,1], NA, coeftest (mod203a10, vcov=cluster.vcov (mod203a10, base83$district))[7,1])
  , c (coeftest (mod203b4d2, vcov=cluster.vcov (mod203b4d2, base83$district))[1:6,1], NA, coeftest (mod203b4d2, vcov=cluster.vcov (mod203b4d2, base83$district))[7,1])
  , c (coeftest (mod203c4d2, vcov=cluster.vcov (mod203c4d2, base83$district))[1:6,1], NA, coeftest (mod203c4d2, vcov=cluster.vcov (mod203c4d2, base83$district))[7,1]) ))
(tab.ses02 <- cbind (
  c (coeftest (mod203a4, vcov=cluster.vcov (mod203a4, base$district))[,2])
  , c (coeftest (mod203b4, vcov=cluster.vcov (mod203b4, base$district))[,2])
  , c (coeftest (mod203c4, vcov=cluster.vcov (mod203c4, base$district))[,2])
  , c (coeftest (mod201j2, vcov=cluster.vcov (mod201j2, base[!is.na (base$Nbills1),]$district))[,2])
  , c (coeftest (mod203b2, vcov=cluster.vcov (mod203b2, base[!is.na (base$Nbills1),]$district))[,2])
  , c (coeftest (mod203c2, vcov=cluster.vcov (mod203c2, base[!is.na (base$Nbills1),]$district))[,2])
  , c (coeftest (mod203a10, vcov=cluster.vcov (mod203a10, base83$district))[1:6,2], NA, coeftest (mod203a10, vcov=cluster.vcov (mod203a10, base83$district))[7,2])
  , c (coeftest (mod203b4d2, vcov=cluster.vcov (mod203b4d2, base83$district))[1:6,2], NA, coeftest (mod203b4d2, vcov=cluster.vcov (mod203b4d2, base83$district))[7,2])
  , c (coeftest (mod203c4d2, vcov=cluster.vcov (mod203c4d2, base83$district))[1:6,2], NA, coeftest (mod203c4d2, vcov=cluster.vcov (mod203c4d2, base83$district))[7,2]) ))

# Panel b
(tab.coefs03 <- cbind (
  c (coeftest (mod301, vcov=cluster.vcov (mod301, base[base$died==0 & base$resigned==0 & base$wonexect1==0,]$district))[,1], rep (NA, 6))
  , c (coeftest (mod301a, vcov=cluster.vcov (mod301a, base[base$died==0 & base$resigned==0 & base$wonexect1==0,]$district))[,1], rep (NA, 4))
  , c (coeftest (mod301j2, vcov=cluster.vcov (mod301j2, base[base$died==0 & base$resigned==0 & base$wonexect1==0,]$district))[c (1:3,8,4:7),1])
  , c (coeftest (mod303a2, vcov=cluster.vcov (mod303a2, base[base$died==0 & base$resigned==0 & base$wonexect1==0,]$district))[c (1:3,8,4:7),1])
  , c (coeftest (mod303c2, vcov=cluster.vcov (mod303c2, base[base$died==0 & base$resigned==0 & base$wonexect1==0,]$district))[c (1:3,8,4:7),1])
  , c (coeftest (mod304a, vcov=cluster.vcov (mod304a, base[base$died==0 & base$resigned==0 & base$wonexect1==0,]$district))[,1], rep (NA, 4))
  , c (coeftest (mod304j2, vcov=cluster.vcov (mod304j2, base[base$died==0 & base$resigned==0 & base$wonexect1==0,]$district))[c (1:3,8,4:7),1])
  , c (coeftest (mod302j, vcov=cluster.vcov (mod302j, base83[base83$died==0 & base83$resigned==0,]$district))[,1], rep (NA, 6))
  , c (coeftest (mod302j5, vcov=cluster.vcov (mod302j5, base83[base83$died==0 & base83$resigned==0,]$district))[1:2,1], rep (NA, 2), coeftest (mod302j5, vcov=cluster.vcov (mod302j5, base83[base83$died==0 & base83$resigned==0,]$district))[3:6,1]) ))
(tab.ses03 <- cbind (
  c (coeftest (mod301, vcov=cluster.vcov (mod301, base[base$died==0 & base$resigned==0 & base$wonexect1==0,]$district))[,2], rep (NA, 6))
  , c (coeftest (mod301a, vcov=cluster.vcov (mod301a, base[base$died==0 & base$resigned==0 & base$wonexect1==0,]$district))[,2], rep (NA, 4))
  , c (coeftest (mod301j2, vcov=cluster.vcov (mod301j2, base[base$died==0 & base$resigned==0 & base$wonexect1==0,]$district))[c (1:3,8,4:7),2])
  , c (coeftest (mod303a2, vcov=cluster.vcov (mod303a2, base[base$died==0 & base$resigned==0 & base$wonexect1==0,]$district))[c (1:3,8,4:7),2])
  , c (coeftest (mod303c2, vcov=cluster.vcov (mod303c2, base[base$died==0 & base$resigned==0 & base$wonexect1==0,]$district))[c (1:3,8,4:7),2])
  , c (coeftest (mod304a, vcov=cluster.vcov (mod304a, base[base$died==0 & base$resigned==0 & base$wonexect1==0,]$district))[,2], rep (NA, 4))
  , c (coeftest (mod304j2, vcov=cluster.vcov (mod304j2, base[base$died==0 & base$resigned==0 & base$wonexect1==0,]$district))[c (1:3,8,4:7),2])
  , c (coeftest (mod302j, vcov=cluster.vcov (mod302j, base83[base83$died==0 & base83$resigned==0,]$district))[,2], rep (NA, 6))
  , c (coeftest (mod302j5, vcov=cluster.vcov (mod302j5, base83[base83$died==0 & base83$resigned==0,]$district))[1:2,2], rep (NA, 2), coeftest (mod302j5, vcov=cluster.vcov (mod302j5, base83[base83$died==0 & base83$resigned==0,]$district))[3:6,2]) ))

# merging everything
(tab.coefs <- rbind (tab.coefs02, tab.coefs03))
(tab.ses <- rbind (tab.ses02, tab.ses03))
odd <- seq (1, nrow (tab.coefs)*2, by=2)
even <- seq (2, nrow (tab.coefs)*2, by=2)

(tab.results <- data.frame (rbind (tab.coefs, tab.ses)))
rownames (tab.results) <- c (odd, even)
tab.results <- tab.results[order (as.numeric (rownames (tab.results))),]

rows <- c ("Intercept", "", "Midterm", "", "Female", "", "Magnitude (log)", "", "PJ bloc", "", "UCR bloc", "", "Former executive", "", "Committee chair", ""           
           ,  "Intercept", "", "Midterm", "", "Lost election", "", "Midterm x", "Lost election", "Female", "", "Magnitude (log)", "", "PJ bloc", "", "UCR bloc", "")

(aics2 <- c (AIC (mod203a4), AIC (mod203b4), AIC (mod203c4), AIC (mod201j2), AIC (mod203b2), AIC (mod203c2), AIC (mod203a10), AIC (mod203b4d2), AIC (mod203c4d2)))
(aics3 <- c (AIC (mod301), AIC (mod301a), AIC (mod301j2), AIC (mod303a2), AIC (mod303c2), AIC (mod304a), AIC (mod304j2), AIC (mod302j), AIC (mod302j5)))
(bics2 <- c (BIC (mod203a4), BIC (mod203b4), BIC (mod203c4), BIC (mod201j2), BIC (mod203b2), BIC (mod203c2), BIC (mod203a10), BIC (mod203b4d2), BIC (mod203c4d2)))
(bics3 <- c (BIC (mod301), BIC (mod301a), BIC (mod301j2), BIC (mod303a2), BIC (mod303c2), BIC (mod304a), BIC (mod304j2), BIC (mod302j), BIC (mod302j5)))
(logliks2 <- c (logLik (mod203a4), logLik (mod203b4), logLik (mod203c4), logLik (mod201j2), logLik (mod203b2), logLik (mod203c2), logLik (mod203a10), logLik (mod203b4d2), logLik (mod203c4d2)))
(logliks3 <- c (logLik (mod301), logLik (mod301a), logLik (mod301j2), logLik (mod303a2), logLik (mod303c2), logLik (mod304a), logLik (mod304j2), logLik (mod302j), logLik (mod302j5)))
(deviances2 <- c (deviance (mod203a4), deviance (mod203b4), deviance (mod203c4), deviance (mod201j2), deviance (mod203b2), deviance (mod203c2), deviance (mod203a10), deviance (mod203b4d2), deviance (mod203c4d2)))
(deviances3 <- c (deviance (mod301), deviance (mod301a), deviance (mod301j2), deviance (mod303a2), deviance (mod303c2), deviance (mod304a), deviance (mod304j2), deviance (mod302j), deviance (mod302j5)))

(n.obs2 <- c (mod203a4$df.null + 1, mod203b4$df.null + 1, mod203c4$df.null + 1, mod201j2$df.null + 1, mod203b2$df.null + 1, mod203c2$df.null + 1, mod203a10$df.null + 1, mod203b4d2$df.null + 1, mod203c4d2$df.null + 1))
(n.obs3 <- c (mod301$df.null + 1, mod301a$df.null + 1, mod301j2$df.null + 1, mod303a2$df.null + 1, mod303c2$df.null + 1, mod304a$df.null + 1, mod304j2$df.null + 1, mod302j$df.null + 1, mod302j5$df.null + 1))

(theta2 <- c (mod203a4$theta, mod203b4$theta, mod203c4$theta, mod201j2$theta, mod203b2$theta, mod203c2$theta, mod203a10$theta, mod203b4d2$theta, mod203c4d2$theta))
(se.theta2 <- c (mod203a4$SE.theta, mod203b4$SE.theta, mod203c4$SE.theta, mod201j2$SE.theta, mod203b2$SE.theta, mod203c2$SE.theta, mod203a10$SE.theta, mod203b4d2$SE.theta, mod203c4d2$SE.theta))

Header0 <- paste ("\\multicolumn{10}{c}{} \\\\ \n")
Header0l <- paste ("\\hline \\multicolumn{10}{c}{} \\\\ \n")
Header1 <- paste (" & \\multicolumn{3}{c}{First 2 years} & \\multicolumn{3}{c}{Whole term} & \\multicolumn{3}{c}{1983 class} \\\\ \n")
Header2 <- paste (" & \\multicolumn{1}{c}{total} & \\multicolumn{1}{c}{prov.} & \\multicolumn{1}{c}{muni.} & \\multicolumn{1}{c}{total} & \\multicolumn{1}{c}{prov.} & \\multicolumn{1}{c}{muni.} & \\multicolumn{1}{c}{total} & \\multicolumn{1}{c}{prov.} & \\multicolumn{1}{c}{muni.} \\\\ \n")
Header3 <- paste ("\\multicolumn{1}{l}{\\textbf{(a) Bill submission}} & \\multicolumn{1}{c}{bills} & \\multicolumn{1}{c}{target} & \\multicolumn{1}{c}{target} & \\multicolumn{1}{c}{bills} & \\multicolumn{1}{c}{target} & \\multicolumn{1}{c}{target} & \\multicolumn{1}{c}{bills} & \\multicolumn{1}{c}{target} & \\multicolumn{1}{c}{target} \\\\ \n")
Header4 <- paste ("\\multicolumn{1}{c}{\\textbf{~~~~(neg. binomial)}} & \\multicolumn{1}{c}{(2.1)} & \\multicolumn{1}{c}{(2.2)} & \\multicolumn{1}{c}{(2.3)} & \\multicolumn{1}{c}{(2.4)} & \\multicolumn{1}{c}{(2.5)} & \\multicolumn{1}{c}{(2.6)} & \\multicolumn{1}{c}{(2.7)} & \\multicolumn{1}{c}{(2.8)} & \\multicolumn{1}{c}{(2.9)} \\\\ \\hline \\hline \n")
Header5 <- paste ("\\multicolumn{1}{l}{\\textbf{(b) Reelection}} & \\multicolumn{3}{c}{Lost any election} & \\multicolumn{1}{c}{gov.} & \\multicolumn{1}{c}{muni.} & \\multicolumn{2}{c}{Reelected} & \\multicolumn{2}{c}{1983 class} \\\\ \n")
Header7 <- paste ("\\multicolumn{1}{l}{\\textbf{~~~~~(logit)}} & \\multicolumn{1}{c}{(3.1)} & \\multicolumn{1}{c}{(3.2)} & \\multicolumn{1}{c}{(3.3)} & \\multicolumn{1}{c}{(3.4)} & \\multicolumn{1}{c}{(3.5)} & \\multicolumn{1}{c}{(3.6)} & \\multicolumn{1}{c}{(3.7)} & \\multicolumn{1}{c}{(3.8)} & \\multicolumn{1}{c}{(3.9)} \\\\ \\hline \\hline \n")
add1 <- paste ("dispersion ($\\theta$) & \\multicolumn{1}{c}{", round (theta2[1], 2), "} & \\multicolumn{1}{c}{", round (theta2[2], 2), "} & \\multicolumn{1}{c}{", round (theta2[3], 2), "} & \\multicolumn{1}{c}{", round (theta2[4], 2), "} & \\multicolumn{1}{c}{", round (theta2[5], 2), "} & \\multicolumn{1}{c}{", round (theta2[6], 2), "} & \\multicolumn{1}{c}{", round (theta2[7], 2), "} & \\multicolumn{1}{c}{", round (theta2[8], 2), "} & \\multicolumn{1}{c}{", round (theta2[9], 2), "} \\\\ \n")
add2 <- paste (" & \\multicolumn{1}{c}{", round (se.theta2[1], 2), "} & \\multicolumn{1}{c}{", round (se.theta2[2], 2), "} & \\multicolumn{1}{c}{", round (se.theta2[3], 2), "} & \\multicolumn{1}{c}{", round (se.theta2[4], 2), "} & \\multicolumn{1}{c}{", round (se.theta2[5], 2), "} & \\multicolumn{1}{c}{", round (se.theta2[6], 2), "} & \\multicolumn{1}{c}{", round (se.theta2[7], 2), "} & \\multicolumn{1}{c}{", round (se.theta2[8], 2), "} & \\multicolumn{1}{c}{", round (se.theta2[9], 2), "} \\\\ \n")
add3 <- paste ("AIC & \\multicolumn{1}{c}{", round (aics2[1], 1), "} & \\multicolumn{1}{c}{", round (aics2[2], 1), "} & \\multicolumn{1}{c}{", round (aics2[3], 1), "} & \\multicolumn{1}{c}{", round (aics2[4], 1), "} & \\multicolumn{1}{c}{", round (aics2[5], 1), "} & \\multicolumn{1}{c}{", round (aics2[6], 1), "} & \\multicolumn{1}{c}{", round (aics2[7], 1), "} & \\multicolumn{1}{c}{", round (aics2[8], 1), "} & \\multicolumn{1}{c}{", round (aics2[9], 1), "} \\\\ \n")
add4 <- paste ("BIC & \\multicolumn{1}{c}{", round (bics2[1], 1), "} & \\multicolumn{1}{c}{", round (bics2[2], 1), "} & \\multicolumn{1}{c}{", round (bics2[3], 1), "} & \\multicolumn{1}{c}{", round (bics2[4], 1), "} & \\multicolumn{1}{c}{", round (bics2[5], 1), "} & \\multicolumn{1}{c}{", round (bics2[6], 1), "} & \\multicolumn{1}{c}{", round (bics2[7], 1), "} & \\multicolumn{1}{c}{", round (bics2[8], 1), "} & \\multicolumn{1}{c}{", round (bics2[9], 1), "} \\\\ \n")
add5 <- paste ("log-Likelihood & \\multicolumn{1}{c}{", round (logliks2[1], 1), "} & \\multicolumn{1}{c}{", round (logliks2[2], 1), "} & \\multicolumn{1}{c}{", round (logliks2[3], 1), "} & \\multicolumn{1}{c}{", round (logliks2[4], 1), "} & \\multicolumn{1}{c}{", round (logliks2[5], 1), "} & \\multicolumn{1}{c}{", round (logliks2[6], 1), "} & \\multicolumn{1}{c}{", round (logliks2[7], 1), "} & \\multicolumn{1}{c}{", round (logliks2[8], 1), "} & \\multicolumn{1}{c}{", round (logliks2[9], 1), "} \\\\ \n")
add6 <- paste ("Deviance & \\multicolumn{1}{c}{", round (deviances2[1], 1), "} & \\multicolumn{1}{c}{", round (deviances2[2], 1), "} & \\multicolumn{1}{c}{", round (deviances2[3], 1), "} & \\multicolumn{1}{c}{", round (deviances2[4], 1), "} & \\multicolumn{1}{c}{", round (deviances2[5], 1), "} & \\multicolumn{1}{c}{", round (deviances2[6], 1), "} & \\multicolumn{1}{c}{", round (deviances2[7], 1), "} & \\multicolumn{1}{c}{", round (deviances2[8], 1), "} & \\multicolumn{1}{c}{", round (deviances2[9], 1), "} \\\\ \n")
add7 <- paste ("$N$ & \\multicolumn{1}{c}{", round (n.obs2[1], 1), "} & \\multicolumn{1}{c}{", round (n.obs2[2], 1), "} & \\multicolumn{1}{c}{", round (n.obs2[3], 1), "} & \\multicolumn{1}{c}{", round (n.obs2[4], 1), "} & \\multicolumn{1}{c}{", round (n.obs2[5], 1), "} & \\multicolumn{1}{c}{", round (n.obs2[6], 1), "} & \\multicolumn{1}{c}{", round (n.obs2[7], 1), "} & \\multicolumn{1}{c}{", round (n.obs2[8], 1), "} & \\multicolumn{1}{c}{", round (n.obs2[9], 1), "} \\\\ \\hline \n")
add9 <- paste ("AIC & \\multicolumn{1}{c}{", round (aics3[1], 1), "} & \\multicolumn{1}{c}{", round (aics3[2], 1), "} & \\multicolumn{1}{c}{", round (aics3[3], 1), "} & \\multicolumn{1}{c}{", round (aics3[4], 1), "} & \\multicolumn{1}{c}{", round (aics3[5], 1), "} & \\multicolumn{1}{c}{", round (aics3[6], 1), "} & \\multicolumn{1}{c}{", round (aics3[7], 1), "} & \\multicolumn{1}{c}{", round (aics3[8], 1), "} & \\multicolumn{1}{c}{", round (aics3[9], 1), "} \\\\ \n")
add10 <- paste ("BIC & \\multicolumn{1}{c}{", round (bics3[1], 1), "} & \\multicolumn{1}{c}{", round (bics3[2], 1), "} & \\multicolumn{1}{c}{", round (bics3[3], 1), "} & \\multicolumn{1}{c}{", round (bics3[4], 1), "} & \\multicolumn{1}{c}{", round (bics3[5], 1), "} & \\multicolumn{1}{c}{", round (bics3[6], 1), "} & \\multicolumn{1}{c}{", round (bics3[7], 1), "} & \\multicolumn{1}{c}{", round (bics3[8], 1), "} & \\multicolumn{1}{c}{", round (bics3[9], 1), "} \\\\ \n")
add11 <- paste ("log-Likelihood & \\multicolumn{1}{c}{", round (logliks3[1], 1), "} & \\multicolumn{1}{c}{", round (logliks3[2], 1), "} & \\multicolumn{1}{c}{", round (logliks3[3], 1), "} & \\multicolumn{1}{c}{", round (logliks3[4], 1), "} & \\multicolumn{1}{c}{", round (logliks3[5], 1), "} & \\multicolumn{1}{c}{", round (logliks3[6], 1), "} & \\multicolumn{1}{c}{", round (logliks3[7], 1), "} & \\multicolumn{1}{c}{", round (logliks3[8], 1), "} & \\multicolumn{1}{c}{", round (logliks3[9], 1), "} \\\\ \n")
add12 <- paste ("Deviance & \\multicolumn{1}{c}{", round (deviances3[1], 1), "} & \\multicolumn{1}{c}{", round (deviances3[2], 1), "} & \\multicolumn{1}{c}{", round (deviances3[3], 1), "} & \\multicolumn{1}{c}{", round (deviances3[4], 1), "} & \\multicolumn{1}{c}{", round (deviances3[5], 1), "} & \\multicolumn{1}{c}{", round (deviances3[6], 1), "} & \\multicolumn{1}{c}{", round (deviances3[7], 1), "} & \\multicolumn{1}{c}{", round (deviances3[8], 1), "} & \\multicolumn{1}{c}{", round (deviances3[9], 1), "} \\\\ \n")
add13 <- paste ("$N$ & \\multicolumn{1}{c}{", round (n.obs3[1], 1), "} & \\multicolumn{1}{c}{", round (n.obs3[2], 1), "} & \\multicolumn{1}{c}{", round (n.obs3[3], 1), "} & \\multicolumn{1}{c}{", round (n.obs3[4], 1), "} & \\multicolumn{1}{c}{", round (n.obs3[5], 1), "} & \\multicolumn{1}{c}{", round (n.obs3[6], 1), "} & \\multicolumn{1}{c}{", round (n.obs3[7], 1), "} & \\multicolumn{1}{c}{", round (n.obs3[8], 1), "} & \\multicolumn{1}{c}{", round (n.obs3[9], 1), "} \\\\ \\hline \n")
Bottom1 <- paste ("\\multicolumn{10}{l}{(Clustered standard errors in parentheses.)} \\\\ \n")

addtorow <- list()
addtorow$pos <- list()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 0
addtorow$pos[[6]] <- 16
addtorow$pos[[7]] <- 16
addtorow$pos[[8]] <- 16
addtorow$pos[[9]] <- 16
addtorow$pos[[10]] <- 16
addtorow$pos[[11]] <- 16
addtorow$pos[[12]] <- 16
addtorow$pos[[13]] <- 16
addtorow$pos[[14]] <- 16
addtorow$pos[[15]] <- 16
addtorow$pos[[16]] <- 16
addtorow$pos[[17]] <- 16
addtorow$pos[[18]] <- 32
addtorow$pos[[19]] <- 32
addtorow$pos[[20]] <- 32
addtorow$pos[[21]] <- 32
addtorow$pos[[22]] <- 32
addtorow$pos[[23]] <- 32
addtorow$pos[[24]] <- 32
addtorow$pos[[25]] <- 32
addtorow$command <- c (Header0, Header1, Header2, Header3, Header4
                       , add1, add2, Header0l, add3, add4, add5, add6, Header0, add7
                       , Header0, Header5, Header7
                       , Header0l, add9, add10, add11, add12, Header0, add13
                       , Bottom1)
print (xtable (  cbind (rows, tab.results)
                 , align=c("l","l","c","c","c","c","c","c","c","c","c")
                 , digits=2
                 , caption="{\\small The electoral calendar and legislative behavior in Argentina, 1983-2007.}"
                 , label="T:results02")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow  )



### Replicating Table B1 (descriptive statistics, Appendix B)

# Main sample
(tab.descriptive01 <- round (rbind (
  
  ## Main explanatory variables
  with (base, rbind (
    c (mean (midterm, na.rm=TRUE), sd (midterm, na.rm=TRUE), min (midterm, na.rm=TRUE), max (midterm, na.rm=TRUE))
    , c (mean (lostexect1, na.rm=TRUE), sd (lostexect1, na.rm=TRUE), min (lostexect1, na.rm=TRUE), max (lostexect1, na.rm=TRUE))
    
    ## Dependent variables (1): quality
    , c (mean (Exec.experience, na.rm=TRUE), sd (Exec.experience, na.rm=TRUE), min (Exec.experience, na.rm=TRUE), max (Exec.experience, na.rm=TRUE))
    , c (mean (gov.before, na.rm=TRUE), sd (gov.before, na.rm=TRUE), min (gov.before, na.rm=TRUE), max (gov.before, na.rm=TRUE))
    , c (mean (exec.before, na.rm=TRUE), sd (exec.before, na.rm=TRUE), min (exec.before, na.rm=TRUE), max (exec.before, na.rm=TRUE))
    , c (mean (prevmayor, na.rm=TRUE), sd (prevmayor, na.rm=TRUE), min (prevmayor, na.rm=TRUE), max (prevmayor, na.rm=TRUE))
    , c (mean (Exec.experience1, na.rm=TRUE), sd (Exec.experience1, na.rm=TRUE), min (Exec.experience1, na.rm=TRUE), max (Exec.experience1, na.rm=TRUE))
    , c (mean (Exec.experience2, na.rm=TRUE), sd (Exec.experience2, na.rm=TRUE), min (Exec.experience2, na.rm=TRUE), max (Exec.experience2, na.rm=TRUE))))
  
  ## Dependent variables (2): Bill submission
  , with (base[!is.na (base$Nbills1),], rbind (
    c (mean (Nbills, na.rm=TRUE), sd (Nbills, na.rm=TRUE), min (Nbills, na.rm=TRUE), max (Nbills, na.rm=TRUE))
    , c (mean (Nbills1, na.rm=TRUE), sd (Nbills1, na.rm=TRUE), min (Nbills1, na.rm=TRUE), max (Nbills1, na.rm=TRUE))
    , c (mean (provorig, na.rm=TRUE), sd (provorig, na.rm=TRUE), min (provorig, na.rm=TRUE), max (provorig, na.rm=TRUE))
    , c (mean (provorig1, na.rm=TRUE), sd (provorig1, na.rm=TRUE), min (provorig1, na.rm=TRUE), max (provorig1, na.rm=TRUE))
    , c (mean (muniorigen, na.rm=TRUE), sd (muniorigen, na.rm=TRUE), min (muniorigen, na.rm=TRUE), max (muniorigen, na.rm=TRUE))
    , c (mean (muniorigen1, na.rm=TRUE), sd (muniorigen1, na.rm=TRUE), min (muniorigen1, na.rm=TRUE), max (muniorigen1, na.rm=TRUE))))
  
  ## Dependent variables (3): Reelection as legislator
  , with (base, rbind (
    c (mean (runReel, na.rm=TRUE), sd (runReel, na.rm=TRUE), min (runReel, na.rm=TRUE), max (runReel, na.rm=TRUE))
       , c (mean (reelected, na.rm=TRUE), sd (reelected, na.rm=TRUE), min (reelected, na.rm=TRUE), max (reelected, na.rm=TRUE))
    
    ## Controls
    , c (mean (female, na.rm=TRUE), sd (female, na.rm=TRUE), min (female, na.rm=TRUE), max (female, na.rm=TRUE))
    , c (mean (mag, na.rm=TRUE), sd (mag, na.rm=TRUE), min (mag, na.rm=TRUE), max (mag, na.rm=TRUE))
    , c (mean (delegation, na.rm=TRUE), sd (delegation, na.rm=TRUE), min (delegation, na.rm=TRUE), max (delegation, na.rm=TRUE))
    , c (mean (compresi, na.rm=TRUE), sd (compresi, na.rm=TRUE), min (compresi, na.rm=TRUE), max (compresi, na.rm=TRUE))
    , c (mean (copartisan.presid, na.rm=TRUE), sd (copartisan.presid, na.rm=TRUE), min (copartisan.presid, na.rm=TRUE), max (copartisan.presid, na.rm=TRUE))
    , c (mean (copartisan.gover, na.rm=TRUE), sd (copartisan.gover, na.rm=TRUE), min (copartisan.gover, na.rm=TRUE), max (copartisan.gover, na.rm=TRUE))
    , c (mean (bloque_pj, na.rm=TRUE), sd (bloque_pj, na.rm=TRUE), min (bloque_pj, na.rm=TRUE), max (bloque_pj, na.rm=TRUE))
    , c (mean (bloque_ucr, na.rm=TRUE), sd (bloque_ucr, na.rm=TRUE), min (bloque_ucr, na.rm=TRUE), max (bloque_ucr, na.rm=TRUE))
    , c (mean (execcandt1, na.rm=TRUE), sd (execcandt1, na.rm=TRUE), min (execcandt1, na.rm=TRUE), max (execcandt1, na.rm=TRUE))
  ))), 2))

# 1983 class
(tab.descriptive02 <- round (rbind (
  
  ## Main explanatory variables
  with (base83, rbind (
    c (mean (midterm, na.rm=TRUE), sd (midterm, na.rm=TRUE), min (midterm, na.rm=TRUE), max (midterm, na.rm=TRUE))
  , rep (NA, 4)))
  
  ## Dependent variables (1): quality
    , rbind (
      rep (NA, 4)
    , rep (NA, 4)
    , rep (NA, 4)
    , rep (NA, 4)
    , rep (NA, 4)
    , rep (NA, 4))
  
  ## Dependent variables (2): Bill submission
  , with (base83[!is.na (base83$Nbills1),], rbind (
    c (mean (Nbills, na.rm=TRUE), sd (Nbills, na.rm=TRUE), min (Nbills, na.rm=TRUE), max (Nbills, na.rm=TRUE))
    , c (mean (Nbills1, na.rm=TRUE), sd (Nbills1, na.rm=TRUE), min (Nbills1, na.rm=TRUE), max (Nbills1, na.rm=TRUE))
    , c (mean (provorig, na.rm=TRUE), sd (provorig, na.rm=TRUE), min (provorig, na.rm=TRUE), max (provorig, na.rm=TRUE))
    , c (mean (provorig1, na.rm=TRUE), sd (provorig1, na.rm=TRUE), min (provorig1, na.rm=TRUE), max (provorig1, na.rm=TRUE))
    , c (mean (muniorigen, na.rm=TRUE), sd (muniorigen, na.rm=TRUE), min (muniorigen, na.rm=TRUE), max (muniorigen, na.rm=TRUE))
    , c (mean (muniorigen1, na.rm=TRUE), sd (muniorigen1, na.rm=TRUE), min (muniorigen1, na.rm=TRUE), max (muniorigen1, na.rm=TRUE))))
  
  ## Dependent variables (3): Reelection as legislator
  , with (base83, rbind (
    c (mean (runReel, na.rm=TRUE), sd (runReel, na.rm=TRUE), min (runReel, na.rm=TRUE), max (runReel, na.rm=TRUE))
    , c (mean (reelected, na.rm=TRUE), sd (reelected, na.rm=TRUE), min (reelected, na.rm=TRUE), max (reelected, na.rm=TRUE))
    
    ## Controls
    , c (mean (female, na.rm=TRUE), sd (female, na.rm=TRUE), min (female, na.rm=TRUE), max (female, na.rm=TRUE))
    , c (mean (mag, na.rm=TRUE), sd (mag, na.rm=TRUE), min (mag, na.rm=TRUE), max (mag, na.rm=TRUE))
    , c (mean (delegation, na.rm=TRUE), sd (delegation, na.rm=TRUE), min (delegation, na.rm=TRUE), max (delegation, na.rm=TRUE))
    , c (mean (compresi, na.rm=TRUE), sd (compresi, na.rm=TRUE), min (compresi, na.rm=TRUE), max (compresi, na.rm=TRUE))
    , c (mean (copartisan.presid, na.rm=TRUE), sd (copartisan.presid, na.rm=TRUE), min (copartisan.presid, na.rm=TRUE), max (copartisan.presid, na.rm=TRUE))
    , c (mean (copartisan.gover, na.rm=TRUE), sd (copartisan.gover, na.rm=TRUE), min (copartisan.gover, na.rm=TRUE), max (copartisan.gover, na.rm=TRUE))
    , c (mean (bloque_pj, na.rm=TRUE), sd (bloque_pj, na.rm=TRUE), min (bloque_pj, na.rm=TRUE), max (bloque_pj, na.rm=TRUE))
    , c (mean (bloque_ucr, na.rm=TRUE), sd (bloque_ucr, na.rm=TRUE), min (bloque_ucr, na.rm=TRUE), max (bloque_ucr, na.rm=TRUE))
    , rep (NA, 4)
  ))), 2))

# merging
(tab.descriptive <- cbind (tab.descriptive01, tab.descriptive02))

rows <- c ("Midterm", "Lost election"
           , "Former executive", "Former governor", "Former governor or vice-governor", "Former mayor", "Former executive (W)", "Former executive (W sq.)"
           , "Bills submitted", "Bills submitted (first 2 years)", "Bills, prov. target", "Bills, prov. target (first 2 years)", "Bills, muni. target", "Bills, muni. target (first 2 years)"
           , "Reelection", "Got reelected"
           , "Female", "Magnitude", "Delegation size", "Committee chair", "Copartisan president", "Copartisan governor", "PJ bloc", "UCR bloc", "Ran for executive")

(n.main <- mod101a$df.null + 1)
(n.1983 <- mod203a10$df.null + 1)

Header0 <- paste ("\\multicolumn{9}{c}{} \\\\ \n")
Header1 <- paste ("& \\multicolumn{4}{c}{\\textbf{(a) Main sample ($n=", n.main, "$)}} & \\multicolumn{4}{c}{\\textbf{(b) 1983 sample ($n=", n.1983, "$)}} \\\\ \n")
Header2 <- paste ("\\multicolumn{1}{l}{\\emph{Main explanatory variables}} & \\multicolumn{1}{c}{mean} & \\multicolumn{1}{c}{std. dev.} & \\multicolumn{1}{c}{min.} & \\multicolumn{1}{c}{max.} & \\multicolumn{1}{c}{mean} & \\multicolumn{1}{c}{std. dev.} & \\multicolumn{1}{c}{min.} & \\multicolumn{1}{c}{max.} \\\\ \\hline \\hline \n")
Header4 <- paste ("\\multicolumn{9}{l}{\\emph{Dependent variables (1): Executive experience}} \\\\ \\hline \\hline \n")
Header5 <- paste ("\\multicolumn{9}{l}{\\emph{Dependent variables (2): Bill submission}} \\\\ \\hline \\hline \n")
Header6 <- paste ("\\multicolumn{9}{l}{\\emph{Dependent variables (3): Reelection}} \\\\ \\hline \\hline \n")
Header7 <- paste ("\\multicolumn{9}{l}{\\emph{Control variables}} \\\\ \\hline \\hline \n")

addtorow <- list()
addtorow$pos <- list()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 2
addtorow$pos[[6]] <- 2
addtorow$pos[[7]] <- 8
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 14
addtorow$pos[[10]] <- 14
addtorow$pos[[11]] <- 16
addtorow$pos[[12]] <- 16
addtorow$command <- c (Header0, Header1, Header0, Header2
                       , Header0, Header4
                       , Header0, Header5
                       , Header0, Header6
                       , Header0, Header7)
print (xtable (  cbind (rows, tab.descriptive)
                 , align=c("l","l","r","r","r","r","r","r","r","r")
                 , digits=2
                 , caption="{\\small Descriptive statistics}"
                 , label="T:descriptive")
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="footnotesize"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c (25)
       , add.to.row=addtorow  )
